﻿
//Author: 张亚涛
//createtime:2015-12-24 09:56:48
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "Price" 表的方法;
    /// </summary>
    public class PriceBase
    {
        const string SQL_Select = "select [ID],[PriceType],[OutPrice],[InPrice] from [Price] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回Price表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<Price> GetAllPrice()
        {
            List<Price> priceList = new List<Price>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                Price price = new Price(reader);
                priceList.Add(price);
            }
            reader.Close();
            return priceList;
        }
        /// <summary>
        /// 根据Price表的ID字段返回数据 
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>       
        public static Price GetPriceById(int id)
        {
            Price price = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                price = new Price(reader);
            }
            reader.Close();
            return price;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据Price表的ID字段删除数据 
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>        
        public static int DeletePriceById(int id)
        {
            string sql = "delete from [Price] where [ID] = @Id";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",id)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(Price price)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@Id",price.Id),
            new SqlParameter("@PriceType",price.PriceType),
            new SqlParameter("@OutPrice",price.OutPrice),
            new SqlParameter("@InPrice",price.InPrice)    
            };
            return paras;
        }
        /// <summary>
        /// 根据Price表的ID字段更新数据 
        /// </summary> 
        /// <param name="Price">price</param>
        /// <returns></returns>       
        public static int UpdatePriceById(Price price)
        {
            string sql = "update [Price] set [PriceType] = @PriceType,[OutPrice] = @OutPrice,[InPrice] = @InPrice where [ID] = @Id";
            SqlParameter[] paras = PrepareCommandParameters(price);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向Price表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="price">Price</param>       
        /// <returns></returns>        
        public static int AddPrice(Price price)
        {
            string sql = "insert into [Price]([PriceType],[OutPrice],[InPrice])values(@PriceType,@OutPrice,@InPrice)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(price);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回Price表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllPrice_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}